Midrash for Pirkei Avot 6:9
אָמַר רַבִּי יוֹסֵי בֶן קִסְמָא, פַּעַם אַחַת הָיִיתִי מְהַלֵּךְ בַּדֶּרֶךְ וּפָגַע בִּי אָדָם אֶחָד, וְנָתַן לִי שָׁלוֹם, וְהֶחֱזַרְתִּי לוֹ שָׁלוֹם. אָמַר לִי, רַבִּי, מֵאֵיזֶה מָקוֹם אַתָּה. אָמַרְתִּי לוֹ, מֵעִיר גְּדוֹלָה שֶׁל חֲכָמִים וְשֶׁל סוֹפְרִים אָנִי. אָמַר לִי, רַבִּי, רְצוֹנְךָ שֶׁתָּדוּר עִמָּנוּ בִמְקוֹמֵנוּ, וַאֲנִי אֶתֵּן לְךָ אֶלֶף אֲלָפִים דִּינְרֵי זָהָב וַאֲבָנִים טוֹבוֹת וּמַרְגָּלִיּוֹת. אָמַרְתִּי לוֹ, בְּנִי, אִם אַתָּה נוֹתֵן לִי כָל כֶּסֶף וְזָהָב וַאֲבָנִים טוֹבוֹת וּמַרְגָּלִיּוֹת שֶׁבָּעוֹלָם, אֵינִי דָר אֶלָּא בִמְקוֹם תּוֹרָה. וְלֹא עוֹד, אֶלָּא שֶׁבִּשְׁעַת פְּטִירָתוֹ שֶׁל אָדָם אֵין מְלַוִּין לוֹ לָאָדָם לֹא כֶסֶף וְלֹא זָהָב וְלֹא אֲבָנִים טוֹבוֹת וּמַרְגָּלִיּוֹת, אֶלָּא תוֹרָה וּמַעֲשִׂים טוֹבִים בִּלְבַד, שֶׁנֶּאֱמַר (משלי ו) בְּהִתְהַלֶּכְךָ תַּנְחֶה אֹתָךְ, בְּשָׁכְבְּךָ תִּשְׁמֹר עָלֶיךָ, וַהֲקִיצוֹתָ הִיא תְשִׂיחֶךָ. בְּהִתְהַלֶּכְךָ תַּנְחֶה אֹתָךְ, בָּעוֹלָם הַזֶּה, בְּשָׁכְבְּךָ תִּשְׁמֹר עָלֶיךָ, בַּקֶּבֶר, וַהֲקִיצוֹתָ הִיא תְשִׂיחֶךָ, לָעוֹלָם הַבָּא. וְכֵן כָּתוּב בְּסֵפֶר תְּהִלִּים עַל יְדֵי דָוִד מֶלֶךְ יִשְׂרָאֵל (תהלים קיט), טוֹב לִי תוֹרַת פִּיךָ מֵאַלְפֵי זָהָב וָכָסֶף. וְאוֹמֵר (חגי ב) לִי הַכֶּסֶף וְלִי הַזָּהָב אָמַר ה' צְבָאוֹת:
R. Yossi b. Kisma said: "Once I was walking along the road when a certain man met me and greeted me and I greeted him in return. He asked me: 'Rebbi, where are you from?' I answered: 'From a great city of sages and scribes.' He replied: "Rebbi, please live with us in our place and I will give you thousands upon thousands of golden dinars and precious stones and pearls.' I said to him: 'My son, if you gave me all the silver and gold and precious stones and pearls in the world I would live only in a place of Torah. For when a man dies there accompany him neither silver nor gold nor precious stones nor pearls, but Torah and good deeds alone.'" As it is written (Proverbs 6:22): "In your going forth, it will lead you, in your lying down, it will guard you, and in your waking, it will be your converse." "In your going forth, it will lead you" — in this world; "in your lying down it will guard you" — in the grave; "in your waking, it will be your converse" — in the world to come. And thus is it written in the book of Psalms of David, king of Israel (Psalms 119:72): "Better to me the Torah of Your mouth than thousands in gold and silver," and (Chaggai 2:8): "Mine is the silver and Mine the gold, says the L rd of hosts."